from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from mall import settings


def generate_save_token(openid):
    serializer = Serializer(settings.SECRET_KEY,expires_in=3600)

    data = {'openid':openid}

    token = serializer.dumps(data)

    return token.decode()


def generate_check_token(openid):

    serializer = Serializer(settings.SECRET_KEY,expires_in=3600)

    try:
        data = serializer.loads(openid)

    except Exception:
        return None

    return data